home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15225 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  69 lines

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: wgk@zurich.ibm.com (Keith Whittingham)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Did I Miss Something?
  5. Date: 4 Apr 1996 08:37:27 GMT
  6. Organization: IBM Research, ZRH
  7. Message-ID: <4k01o7$sl2@grimsel.zurich.ibm.com>
  8. References: <N.040396.105136.51@ix.netcom.com>
  9. Reply-To: wgk@zurich.ibm.com
  10. NNTP-Posting-Host: pine.zurich.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.00
  12.  
  13. In <N.040396.105136.51@ix.netcom.com>, jhewett@ix.netcom.com (Jerry Hewett) writes:
  14. >
  15. >class box {
  16. >    int length;
  17. >    int width;
  18. >    char *line_of_text;
  19. >public:
  20. >    box(char *input_line);
  21. >    void set(int new_length, int new_width);
  22. >    int get_area(void);
  23. >};
  24. >box::box(char *input_line)
  25. >{
  26. >    length = 8;
  27. >    width = 8;
  28. >    line_of_text = input_line;
  29. >}
  30. >
  31. >main()
  32. >{
  33. >    box small("small box ");
  34. >
  35. >}
  36. >
  37. >So far (the past three days) I've learned more from the Coronado Enterprises 
  38. >C++ TUTOR than from all of the books I bought that were supposed to help me 
  39.  
  40. There is no memory allocation for the string: your assumption is wrong. 
  41. I think the code you're are being given is not a very good example! 
  42. The constructer would be better written:
  43.  
  44.  
  45. box::box(char *input_line)
  46.   {
  47.   length = 8;
  48.   width = 8;
  49.   line_of_text = strdup(input_line);
  50.   }
  51.  
  52. and a destructor...
  53.  
  54. box::~box()
  55.   {
  56.   free(line_of_text);
  57.   }
  58.  
  59. Of course the C++ biggots will insist on using new and delete instead of
  60. strdup and free but that's all a matter of stlye.
  61.  
  62. In general C++ itself does no more for you than C, no automatic memory
  63. allocation, no garbage collection etc. All in all that should lead to
  64. faster code but you do have to be thorough when writing the stuff.
  65.  
  66. Keith
  67.  
  68.  
  69.